<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\Redis;


class Call extends Base
{
    const CALL_NUMBER_INFO = 'CALL:LIANG:NUMBER:INFO';//靓号
    const CALL_POND = 'CALL:POND';//奖池
    const CALL_POND_EXISTS = 'CALL:POND:EXISTS';//奖池是否存在
    const CALL_AWARD_USER = 'CALL:AWARD:USER';//待开奖
    const CALL_AWARD_MALL_LOG = 'CALL:AWARD:MALL:LOG';//记录
    const CALL_SPEED_NUM = 'CALL:SPEED:NUM';//加速次数
    const CALL_SEND_MSG = 'CALL:SEND:MSG';//发送领奖消息
    const award_mall_id = [
        [ //0级奖池
            127 => 500,
            153 => 500,
            124 => 500,
            306 => 1500,
            304 => 1000,
            261 => 2000,
            252 => 2000,
            310 => 2000,
        ],
        [//1级奖池
            309 => 1001,
            153 => 3000,
            124 => 1999,
            301 => 5000,
            306 => 5000,
            300 => 5000,
            299 => 8000,
            298 => 8000,
            282 => 8000,
            281 => 10000,
            276 => 10000,
            274 => 15000,
            271 => 20000,
        ]
        , [//2级奖池 包含靓号
            321321 => 1,
            309 => 1000,
            153 => 3000,
            124 => 1999,
            301 => 5000,
            306 => 5000,
            300 => 5000,
            299 => 8000,
            298 => 8000,
            282 => 8000,
            281 => 10000,
            276 => 10000,
            274 => 15000,
            271 => 20000,
        ]
    ];//奖池礼物id

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    public function existsPond()
    {
        $key = self::CALL_POND_EXISTS;
        return Redis::GET($key);
    }

    public function createPondExists()
    {
        $key = self::CALL_POND_EXISTS;
        Redis::SET($key, 1);
        return $this->ttlRedisKey($key);
    }

    //生成奖池
    public function createPond($level)
    {
        $key = self::CALL_POND . '-' . $level;
        $mallId = self::award_mall_id[$level];
        $gift_ids = array_keys($mallId);
        $num = array_values($mallId);
        $pond_arr = [];
        foreach ($num as $k => $v) {
            $arr = array_fill(0, $v, $gift_ids[$k]);
            $pond_arr = array_merge($pond_arr, $arr);
        }
        shuffle($pond_arr);
        Redis::LPUSH($key, $pond_arr);
        return $this->ttlRedisKey($key);
    }

    public function setAwardList($user_id, $data)
    {
        $key = self::CALL_AWARD_USER;
        Redis::HSETNX($key, $user_id, $data);
        return $this->ttlRedisKey($key);
    }

    public function getUserCall($user_id)
    {
        $key = self::CALL_AWARD_USER;
        return Redis::HGET($key, $user_id);
    }

    public function getUserCallAll()
    {
        $key = self::CALL_AWARD_USER;
        return Redis::HGETALL($key);
    }

    public function delUserCall($user_id)
    {
        $key = self::CALL_AWARD_USER;
        return Redis::HDEL($key, $user_id);
    }

    //奖池
    public function getPond($level)
    {
        $key = self::CALL_POND . '-' . $level;
        return Redis::LPOP($key);
    }

    //设置靓号
    public function setNumberToUser($user_info)
    {
        $v[self::CALL_NUMBER_INFO] = json_encode($user_info);
        return Redis::MSETNX($v);
    }

    //获奖记录
    public function createAwardLog($user_id, $gift_name, $type)
    {
        $key = self::CALL_AWARD_MALL_LOG . '-' . $user_id;
        $k = $gift_name . '-' . time() . '-' . rand(1, 10000);
        Redis::HSET($key, $k, $type);
        return $this->ttlRedisKey($key);
    }

    //获取中间纪录
    public function getAwardLog($user_id)
    {
        $key = self::CALL_AWARD_MALL_LOG . '-' . $user_id;
        return Redis::HGETALL($key);
    }

    //加速次数
    public function incSpeedNum($user_id)
    {
        $key = self::CALL_SPEED_NUM . '-' . date('Ymd');
        Redis::HINCRBY($key, $user_id, 1);
        return $this->ttlRedisKey($key);
    }

    //获取加速次数
    public function getSpeedNum($user_id)
    {
        $key = self::CALL_SPEED_NUM . '-' . date('Ymd');
        return Redis::HGET($key, $user_id);
    }


    public function msgLog($user_id)
    {
        $key = self::CALL_SEND_MSG . '-' . date('Ymd');
        $this->ttlRedisKey($key);
        return Redis::SADD($key, $user_id);
    }

    public function delMsgLog($user_id)
    {
        $key = self::CALL_SEND_MSG . '-' . date('Ymd');
        return Redis::SREM($key, $user_id);
    }
}